<template name="home">
	<view class="app-bg page">

		<form>
			<view style="padding: 15rpx;">
				<view class="calendar-remark">
					<textarea v-model="privateWord" :placeholder='privatePlaceHolder' maxlength=-1 style="width: 100%;" />
				</view>
				<view class="flex justify-end" style="width: 100%; margin-top: 2rpx;">
					<view class="suspend-btn text-xs text-center round padding-xs" @tap="cleanData('1')">清空</view>
					<view class="suspend-btn text-xs text-center round padding-xs margin-left" @tap="copyData('1')">复制</view>
				</view>
			
			</view>
			
			<view class="flex justify-center margin-xs align-center">
				
				<view class = "flex align-center round bg-primary margin-right"  style="width: 120rpx;height: 120rpx;"
				@tap="changeWord('2')" >
					<image style="height: 40rpx;width: 40rpx;" src="../../static/img/downarrow.png"></image>
					解密
				</view>
				<view class = "flex align-center round bg-primary margin-left"  style="width: 120rpx;height: 120rpx;"
				@tap="changeWord('1')">
					<image style="height: 40rpx;width: 40rpx;" src="../../static/img/uparrow.png"></image>
					加密
				</view>
				
				<text class = "flex align-center round bg-primary text-center text-content padding-xs" style="margin-left: 160rpx;background-color: #5b49e6;"
				@tap="addDemo()">
					示例
				</text>
				
			<!-- 	<view class="flex" style="width:80rpx; margin-left: 160rpx; background-color: #007AFF;">
					<view class = "round text-sm  padding-xs" style="background-color: #00B894;color: #ffff; "
					@tap="addDemo()">
						示例
					</view>
				</view>
				 -->

			</view>
			
			<view style="padding: 15rpx;">
				<view class="calendar-remark">
					<textarea v-model="openWord" maxlength=40 :placeholder='openPlaceHolder'  style="height: 100%; width: 100%;" />
				</view>
				<view class="flex justify-end" style="width: 100%; margin-top: 2rpx;">
					<view class="suspend-btn text-xs text-center round padding-xs" @tap="cleanData('2')">清空</view>
					<view class="suspend-btn text-xs text-center round padding-xs margin-left" @tap="copyData('2')">复制</view>
				</view>
							
			
			</view>
		</form>


	</view>


</template>

<script>
	import dyDatePicker from "../../components/dy-Date/dy-Date.vue";
	import helper from '../../common/helper.js';
	import cvencode from '../../common/cvencode.js';
	import Bus from '../../common/bus.vue';


	export default {
		name: "home",
		components: {
			dyDatePicker
		},
		data() {

			return {
			
				hasUserInfo: false,
				userInfo: {},
				homeInfo: {},
				openWord: '',
				privateWord: '',
				openPlaceHolder: '写点什么',
				privatePlaceHolder: '',
				showType: '',
				// dateDay: '',
				
				modalName: '',
				dataList: []

			};
		},
		created() {
			console.group('created 组件创建完毕状态===============》');
			let that = this;
			//查看是否授权
			uni.getStorage({
				key: 'userInfo',
				success: function(res) {
					console.log(res);
					// if (res.errMsg === 'getStorage:ok' && res.data != null && res.data != '') {
					// 	that.userInfo = res.data;
					// 	that.hasUserInfo = true;
					// 	that.getHomeInfo();
					// } else {
					// 	that.hasUserInfo = false;
					// }
				},
				fail: function(e) {
					console.log(e);
				}
			});

			Bus.$on('userInfo', target => {
				console.log('home bus');
				let that = this;
				//查看是否授权
				uni.getStorage({
					key: 'userInfo',
					success: function(res) {
						console.log(res);
						if (res.errMsg === 'getStorage:ok' && res.data != null && res.data != '') {
							that.userInfo = res.data;
							that.hasUserInfo = true;
							that.getHomeInfo();
						} else {
							that.hasUserInfo = false;
						}
					},
					fail: function(e) {
						console.log(e);
					}
				});
			});

		},
		beforeDestroy() {
			console.log('home beforeDestroy');
			Bus.$off('userInfo');
		},

		methods: {
			setUserInfo() {

			},
			// showModal(e) {
			// 	this.modalName = 'bottomModal'
			// },
			// hideModal(e) {
			// 	this.modalName = ''
			// },
					
			addDemo(){
				this.openWord = '';
				//我是课表
				this.privateWord = '友善爱国公正爱国爱国敬业民主诚信自由公正敬业爱国诚信富强友善敬业诚信自由爱国友善自由友善敬业诚信民主诚信自由友善爱国爱国诚信富强民主友善自由爱国'
				this.openWord='课表是我 （最多能写40个字哦）'
				uni.showToast({
				    title: '请点击加密或解密按钮',
				    duration: 2000
				});
			},


			/**
			 * 加密 解密
			 * @param {Object} type
			 */
			changeWord(type){
				if(1 == type){
					//加密
					var Encrypt = cvencode.encode(this.openWord);
					this.privateWord = Encrypt;
					console.log(Encrypt);
				}else{
					//解密
					var Decrypt = cvencode.decode(this.privateWord); 
					this.openWord = Decrypt;
					if(Decrypt =='' || Decrypt==undefined){
						uni.showToast({
						    title: '转换失败',
						    duration: 2000
						});
					}
					console.log(Decrypt);
				}
			},
			
			cleanData(type){
				if(1==type){
					//输入框1
					this.privateWord = '';
				}else{
					//输入框2
					this.openWord = '';
				}
				
			},
				
			copyData(type){
				var data = ''
				if(1==type){
					//输入框1
					data = this.privateWord;
					
				}else{
					//输入框2
					data = this.openWord;
				}	
				uni.setClipboardData({
				    data: data,
				    success: function () {
				        console.log('success');
						uni.showToast({
						    title: '内容已复制到剪切板',
						    duration: 2000
						});
				    },
					fail() {
						console.log('fail');
						uni.showToast({
						    title: '复制失败',
						    duration: 2000
						});
					}
				});
					
			},
				
				

			// 调用服务器
			changeWord2(type) {



				let that = this;
				let playod = {
					userId: that.userId,
					monthDay: that.monthDay,
					remark: that.remark
				};

				uni.showLoading({
					title: "加载中"
				});
				uni.request({
					url: helper.api + '/appYmrecord/open/addOrUpdateRemark', //仅为示例，并非真实接口地址。
					method: 'get',
					data: playod,
					header: {
						'Content-Type': 'application/json' //自定义请求头信息
					},
					success: (res) => {
						console.log(res.data);
						uni.hideLoading();
						if (res.data.code === 0) {

							uni.showToast({
								title: "操作成功！",
								duration: 1000
							});
							setTimeout(function() {
								Bus.$emit('calendarRemark', "fromCalendarRemark");
							}, 1100);


							// todo 刷新数据
						} else {
							uni.showModal({
								title: '请求失败！',
								content: '错误原因:' + res.data.msg,
								showCancel: false
							});
						}
					},
					fail: (err) => {
						uni.hideLoading();
						uni.showModal({
							title: '请求失败！',
							content: '错误原因:' + err,
							showCancel: false
						});
					}
				});
			},
			/**
			 * 获取首页信息
			 */
			getHomeInfo() {

				let that = this;
				let playod = {
					userId: that.userInfo.userId,
				};

				uni.showLoading({
					title: "加载中"
				});
				uni.request({
					url: helper.api + '/appYmrecord/open/homeInfo', //仅为示例，并非真实接口地址。
					method: 'get',
					data: playod,
					header: {
						'Content-Type': 'application/json' //自定义请求头信息
					},
					success: (res) => {
						uni.hideLoading();

						if (res.data.code === 0) {
							that.homeInfo = res.data.data;
							that.showType = res.data.data.showType;

							// todo 刷新数据
						} else {
							uni.showModal({
								title: '请求失败！',
								content: '错误原因:' + res.data.msg,
								showCancel: false
							});
							return;
						}
					},
					fail: (err) => {
						uni.hideLoading();
						uni.showModal({
							title: '请求失败！',
							content: '错误原因:' + err,
							showCancel: false
						});
					}
				});
			},

		},


	}
</script>

<style lang="scss">
	.page {
		/* width: 100vh; */

	}

	.calendar-remark {
		width: 100%;
		height: 300rpx;
		padding: 15rpx;
		border: 1px solid;
		margin-top: 50rpx;
		border-radius: 5px;
		border-color: $uni-color-primary;
		color: #333333;

	}
	.suspend-btn{
		background-color: #6C5CE7;
		color: #ffff;
		width: 90rpx;
		position: relative;
		right: 0px;;
	}
</style>
